package com.cloudfun.campusshare.entity;

import com.cloudfun.campusshare.common.security.AuthenticationHolder;
import com.cloudfun.campusshare.common.security.UserAuthentication;
import lombok.Data;

import javax.persistence.*;

/**
 * 创建人持久化实体
 * <p>
 * Created by Huqin on 2020/6/5
 */
@Data
@MappedSuperclass
public class CreatorPersistenceEntity extends BaseEntity {

    @Column(name = "creator_id", columnDefinition = ("varchar(40) default null comment '创建人id'"))
    private String creatorId;

    @Column(columnDefinition = ("varchar(64) default null comment '创建人名称'"))
    private String creatorName;

    @ManyToOne
    @JoinColumn(name = "creator_id", insertable = false, updatable = false)
    private UserEntity creator;

    @PrePersist
    public void setCreator() {
        UserAuthentication userAuth = AuthenticationHolder.getUserAuth();
        if (userAuth == null) {
            return;
        }
        this.creatorId = userAuth.getUserId();
        this.creatorName = userAuth.getUsername();
        extraOperate();
    }

    protected void extraOperate() {
    }
}
